package com.moon.controller;

import com.moon.dao.TOrderMapper;
import com.moon.entity.TOrder;
import com.moon.entity.TUser;
import com.moon.feign.UserFeign;
import com.moon.utils.UserUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/order")
@RestController
public class OrderController {
    @Autowired
    private TOrderMapper tOrderMapper;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/addOrder")
    public String addOrder(@RequestBody TOrder tOrder,HttpServletRequest request){
        System.out.println("进入addOrder方法");
        TUser user = UserUtils.getCurrUserFromHeader("token", TUser.class, request);
        //生成订单
        tOrderMapper.insert(tOrder);
        //通知买家
        rabbitTemplate.convertAndSend("sendEmailExchange", "sendEmail", user.getEmail());
        //通知卖家
        Long sellerId = tOrder.getSellerId();
        String email=userFeign.getemail(sellerId);
        rabbitTemplate.convertAndSend("sendEmailExchange", "sendEmail", email);
        return "success";
    }

//  查询我的订单（我是作为买家）
    @RequestMapping("selectMyOrder")
    public List<TOrder> selectMyOrder(HttpServletRequest request){
        TUser user = UserUtils.getCurrUserFromHeader("token", TUser.class, request);
        TOrder order = new TOrder();
        order.setBuyerId(user.getId());
        List<TOrder> tOrders = tOrderMapper.select(order);
        return tOrders;
    }

//  查询我的订单（我是作为卖家）
    @RequestMapping("selectSellerOrder")
    public List<TOrder> selectSellerOrder(HttpServletRequest request){
        TUser user = UserUtils.getCurrUserFromHeader("token", TUser.class, request);
        TOrder order = new TOrder();
        order.setSellerId(user.getId());
        List<TOrder> tOrders = tOrderMapper.select(order);
        return tOrders;
    }
}
